<?php

namespace Home\Controller;

/**
 * 公共商城类
 */
class BaseMallController extends BaseController {

    function _initialize() {
        parent::_initialize();
        session('visitor',NULL);
        
        //默认进行验证
        $user_id = I('request.user_id', 0, 'intval');
        $sign = I('request.sign');
        if ($user_id && $sign) {
            $tokens = M('AppToken')->where('m.user_id=' . $user_id)->alias('at')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'member m ON m.user_id = at.user_id')
                ->field('m.user_id,user_name,user_no,real_name,token,available,password')->select();
            if (!$tokens) {
                render('10015', '用户未通过验证');
            }
            foreach ($tokens as $token) {
                //验证token
                if (md5(MODULE_NAME . CONTROLLER_NAME . ACTION_NAME . $token['token']) == $sign) {
                    if (!$token['password']) {
                        render('10115', '用户未设置登录密码');
                    }
                    if ($token['available']) {
                        render('10114', '用户被禁用');
                    }
                    session('visitor', $token);
                    return;
                }
            }
        }else{
//            $this->auto_login();
        }
    }

    /*
     * 测试不传sign 以及 user_id 自动登录
     */

    function auto_login() {
        $user_id = intval(I('request.auto_user_id'));
        if ($user_id > 0) {
            $member = M('Member')->where('user_id=' . $user_id)->find();
            if ($member) {
                $data = array(
                    'user_id' => $member['user_id'],
                    'user_name' => $member['user_name'],
                );
                session('visitor', $data);
            }
        }
    }

}

?>
